// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ChartControl.xaml.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Visualization
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Forms.DataVisualization.Charting;

    /// <summary>
    /// Interaction logic for ChartControl.xaml
    /// </summary>
    public partial class ChartControl : UserControl
    {
        /// <summary>
        /// Identifies the ChartDataProperty dependency property, used for data binding.
        /// </summary>
        public static readonly DependencyProperty ChartDataProperty = DependencyProperty.RegisterAttached(
            "ChartData", 
            typeof(object), 
            typeof(ChartControl), 
            new FrameworkPropertyMetadata(null, null, new CoerceValueCallback(CoerceValue)));

        /// <summary>
        /// Initializes a new instance of the ChartControl class.
        /// </summary>
        public ChartControl()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets the embedded winforms chart object. This provides access to the chart configuration (series, axes, labels etc.)
        /// </summary>
        public Chart InnerChart
        {
            get
            {
                return this.chart;
            }
        }

        /// <summary>
        /// Performs data binding between the specified property of the model and the image control.
        /// Parent controls need to call this method to establish databinding.
        /// </summary>
        /// <param name="model">The model (typically a Model Agent) to use as the source object.</param>
        /// <param name="propertyName">The name of a model property to bind to.</param>
        public void BindToModel(object model, string propertyName)
        {
            // bind any one model property to ChartDataProperty, just to trigger the CoerceValue below
            // the property is irrelevant, because the Chart WinForm control has its own binding mechanism
            // However, we still need to bind to a valid property in order to receive source update notifications
            Binding binding = new Binding(propertyName);
            binding.Source = model;
            binding.NotifyOnTargetUpdated = true;
            this.SetBinding(ChartDataProperty, binding);
        }

        /// <summary>
        /// Called in response to an update to a databound data source.
        /// This method is expected to convert the source data to the target property type,
        /// but we use it to simply update the chart.
        /// </summary>
        /// <param name="element">The parent object being notified</param>
        /// <param name="value">The updated value of the source object. Not used.</param>
        /// <returns>The value, unchanged.</returns>
        private static object CoerceValue(DependencyObject element, object value)
        {
            if (element is ChartControl)
            {
                // refresh the chart
                ((ChartControl)element).chart.DataBind();
            }

            return value;
        }
    }
}
